//
//  HHHomeViewController.m
//  YSJ
//
//  Created by 花花 on 2016/12/15.
//  Copyright © 2016年 花花. All rights reserved.
//

#import "HHHomeViewController.h"
#import "HHDetailsViewController.h"
#import "UIBarButtonItem+HHExtension.h"
#import "UIButton+HHExtension.h"
#import "HHHomeCell.h"
#import "PhotoFlowLayout.h"
#import "PhotoCell.h"
@interface HHHomeViewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDataSource>

@property(nonatomic, strong) UIButton *topbtn;
@property(nonatomic, strong) UITableView *tableView;


@property(nonatomic, strong) NSMutableArray *tempArr;

@property(nonatomic, strong) NSMutableArray *homeArr;



@property (nonatomic, strong) NSString *infoString;
@property (nonatomic, strong) NSString *infoPicName;


@end

static NSString *const homeCellID = @"homeCellID";
static NSString *const PhotoCellID = @"PhotoCellID";
@implementation HHHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];


    
    [self setupNav];
        [self loadData];
    [self setupTableView];
    
    [self setupTop];
    


}

-(void)loadData{
    NSString *path =[[NSBundle mainBundle]pathForResource:@"Home.json" ofType:nil];
    
    NSData *data =[NSData dataWithContentsOfFile:path];
    
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    
    NSDictionary *datas= [NSDictionary dictionary];
    datas = [json objectForKey:@"datas"];
    NSMutableArray  *bannerArr=[NSMutableArray array];
    bannerArr =  [datas objectForKey:@"banner"];
    
    self.tempArr =[NSMutableArray array];
    for (int i = 0; i<bannerArr.count; i++) {
        NSString *pic =[bannerArr[i] objectForKey:@"advertImg"];
        [self.tempArr addObject:pic];
    }
    
    NSMutableArray *datatype =[NSMutableArray array];
    
    datatype  = [datas objectForKey:@"data_type"];
    self.homeArr = [NSMutableArray array];
    
    for (int i = 1 ; i<datatype.count; i ++) {
        NSDictionary *dic =datatype[i];
        
        HHHomeModel *model =[[HHHomeModel alloc]initWithDic:dic];
        [self.homeArr addObject:model];
        
    }
    
    NSDictionary *firstData = datatype[0];
    self.infoString = [firstData objectForKey:@"relation_object_title"];
    self.infoPicName = [firstData objectForKey:@"relation_object_image"];
    
    [self.tableView reloadData];


}


- (void)setupNav{
    
    UIImageView *titleImage = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 60, 20)];
    titleImage.image = [UIImage imageNamed:@"YS_food+"];
    self.navigationItem.titleView = titleImage;

    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:@"icon_home_search" highImage:@"icon_home_search_index" target:self action:@selector(searchOnClick)];
}

-(void)searchOnClick{

    HHLog(@"%s",__func__);

}

-(void)setupTop{

    UIButton *topBtn =[[UIButton alloc]initWithFrame:CGRectMake(screen_width-60, screen_height * 0.7, 40, 40)];
    [topBtn setBackgroundImage:[UIImage imageNamed:@"icon_back_top"] forState:UIControlStateNormal];
    [topBtn setHidden:YES];
    [topBtn addTarget:self action:@selector(backTopOnlick) forControlEvents:UIControlEventTouchUpInside];
   
    [self.view addSubview:topBtn];
    _topbtn = topBtn;

}


-(void)backTopOnlick{

    [self.tableView setContentOffset:CGPointZero  animated:YES];
}

-(void)setupTableView{
    
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, screen_width, screen_height-77) style:UITableViewStyleGrouped];
    
    tableView.dataSource = self;
    tableView.delegate = self;
    
    
    UIView *headerView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, screen_width, 200)];
    headerView.backgroundColor =[UIColor orangeColor];
    tableView.tableHeaderView = headerView;
    tableView.rowHeight = 500;
    [tableView registerNib:[UINib nibWithNibName:NSStringFromClass([HHHomeCell class]) bundle:nil] forCellReuseIdentifier:homeCellID];
    [self.view addSubview:tableView];
    _tableView = tableView;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
}


#pragma mark - <UITableViewDataSource,UITableViewDelegate>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.homeArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HHHomeCell *cell =[tableView dequeueReusableCellWithIdentifier:homeCellID];
    cell.model = self.homeArr[indexPath.row];
    
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
       [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [self.navigationController pushViewController:[[HHDetailsViewController alloc]init] animated:YES];
    
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{


    UIView * headerView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, screen_width, 420)];
    headerView.backgroundColor =[UIColor whiteColor];
    UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(0, 5, screen_width, 20)];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = @"YUESHI CHOSEN";
    label.textColor =[UIColor colorWithHexString:ThemeColor];
    label.font =[UIFont systemFontOfSize:15];
    [headerView addSubview:label];
    
    UIImageView *imgView =[[UIImageView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(label.frame)+5, screen_width, headerView.height - label.height -20-200)];
    [imgView sd_setImageWithURL:[NSURL URLWithString:self.infoPicName]];
    
    PhotoFlowLayout *layout =[[PhotoFlowLayout alloc]init];
    
    layout.itemSize = CGSizeMake(160, 200);
    layout.minimumLineSpacing = 0.1;
    CGFloat margin = (screen_width - 160)* 0.5;
    
    layout.sectionInset = UIEdgeInsetsMake(0, margin, 0, margin);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  
    UICollectionView *collectionView =[[UICollectionView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(imgView.frame)+5, screen_width, 200) collectionViewLayout:layout];
    collectionView.dataSource = self;
    collectionView.backgroundColor =[UIColor clearColor];
    collectionView.showsHorizontalScrollIndicator = NO;
    [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([PhotoCell class]) bundle:nil] forCellWithReuseIdentifier:PhotoCellID];
    
    [headerView addSubview:collectionView];
    
    [headerView addSubview:imgView];
    
    
    return headerView;
}

#pragma mark - <UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return 10;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    PhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:PhotoCellID forIndexPath:indexPath];
    
    NSString *imageNames = [NSString stringWithFormat:@"%zd",indexPath.item +1];
    cell.image = [UIImage imageNamed:imageNames];
    
    
    return cell;
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 420;

}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
//    HHLog(@"%f",scrollView.contentOffset.y); //上负下正
    if (scrollView.contentOffset.y > screen_height) {
        self.topbtn.hidden = NO;
    }else{
    
        self.topbtn.hidden = YES;
    }

}


@end

